//
//  BookStoreBooksController.m
//  LineByLine
//
//  Created by 병철 김 on 11. 12. 9..
//  Copyright (c) 2011 __MyCompanyName__. All rights reserved.
//

#import "BookStoreBooksController.h"
#import "BookParserDelegate.h"
#import "CoreDataManager.h"
#import "BookStoreBookViewController.h"


@interface BookStoreBooksController (Private)
- (void) reloadBookStoreData;
@end

@implementation BookStoreBooksController

- (void) loadView {
    [super loadView];
    UIBarButtonItem *refreshButton = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemRefresh target:self action:@selector(reloadBookStoreData)];
    [[self navigationItem] setRightBarButtonItem:refreshButton];
    [refreshButton release];
    
    storeBooks = [[[CoreDataManager sharedCoreDataManager] storeBooks] retain];
    tableView = [[UITableView alloc] initWithFrame:self.view.frame style:UITableViewStylePlain];
    [tableView setDataSource:self];
}

- (void)dealloc {
    [tableView release];
    [storeBooks release];
    [super dealloc];
}

- (void) reloadBookStoreData {
    booksRequest = [[HttpRequest alloc] init];
    [booksRequest setFinishTarget:self action:@selector(didFinishDownBooks:)];
    [booksRequest executeUrl:@"http://linebylinestore.appspot.com/BookStore.xml" method:@"GET" params:nil];
}

- (void) parseScript:(NSData*) xmlData {
    NSXMLParser *xmlParser = [[NSXMLParser alloc] initWithData:xmlData];
    BookParserDelegate *bookParser = [[BookParserDelegate alloc] init];
    [bookParser setContext:[[CoreDataManager sharedCoreDataManager] managedObjectContext]];
    [bookParser setEntityName:@"StoreBook"];
    [xmlParser setDelegate:bookParser];
    
    NSAutoreleasePool *tempPool = [[NSAutoreleasePool alloc] init];
    [xmlParser parse];
    [tempPool drain];
    
    [bookParser release];
    [xmlParser release];
}

- (void) didFinishDownBooks:(HttpRequest*) httpRequest {
    NSData *responseData = [[booksRequest responseData] retain];
    [booksRequest close];
    [self parseScript:responseData];
    [responseData release];
    
    [storeBooks release];
    storeBooks = [[[CoreDataManager sharedCoreDataManager] storeBooks] retain];
    [tableView reloadData];
}

#pragma mark - tableview data source
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    return 1;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return [storeBooks count];
}

- (UITableViewCell *)tableView:(UITableView *)_tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    static NSString *cellId = @"TableViewCell";
    
    UITableViewCell *cell = (UITableViewCell*)[tableView dequeueReusableCellWithIdentifier:cellId];
    if (!cell) {
        cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellId] autorelease];
    }
    
    id book = [storeBooks objectAtIndex:[indexPath row]];
    [[cell textLabel] setText:[book valueForKey:@"title"]];
    return cell;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    id book = [storeBooks objectAtIndex:[indexPath row]];
    BookStoreBookViewController *controller = [[BookStoreBookViewController alloc] init];
    [controller setValue:[book valueForKey:@"bookId"] forKey:@"bookId"];
    [[self navigationController] pushViewController:controller animated:YES];
    [controller release];
}

@end
